package com.achievement.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.achievement.domain.dto.honor.HonorApplyBookMemberDTO;
import com.achievement.domain.entity.HonorApplyBookMemberEntity;
import com.achievement.domain.vo.honor.HonorApplyBookMemberVO;
import com.achievement.mapper.HonorApplyBookMemberMapper;
import com.achievement.mappings.HonorApplyBookMemberMapping;
import com.achievement.service.IHonorApplyBookMemberService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
@Service
public class HonorApplyBookMemberServiceImpl extends ServiceImpl<HonorApplyBookMemberMapper, HonorApplyBookMemberEntity> implements IHonorApplyBookMemberService {

    @Autowired
    private HonorApplyBookMemberMapper honorApplyBookMemberMapper;
    @Autowired
    private HonorApplyBookMemberMapping mapping;
    @Override
    public List<HonorApplyBookMemberVO> honorApplyBookMemberByApplyBookId(String applyBookId) {
        LambdaQueryWrapper<HonorApplyBookMemberEntity> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(HonorApplyBookMemberEntity::getApplyBookId, applyBookId);
        queryWrapper.orderByAsc(HonorApplyBookMemberEntity::getOrderId);
        List<HonorApplyBookMemberEntity> list = this.list(queryWrapper);
        List<HonorApplyBookMemberVO> voList = list.stream().map(mapping::entity2Vo).collect(Collectors.toList());
        return voList;
    }
    @Override
    public List<HonorApplyBookMemberEntity> getListByConditions(HonorApplyBookMemberDTO honorApplyBookMemberDTO) {
        QueryWrapper<HonorApplyBookMemberEntity> queryWrapper = new QueryWrapper();
        List<HonorApplyBookMemberEntity> list = this.list(queryWrapper);
        return list;
    }

    @Override
    public boolean bitchAdd(List<HonorApplyBookMemberDTO> honorApplyBookMemberDTOs,String applyBookId){
        List<HonorApplyBookMemberEntity> entityList = honorApplyBookMemberDTOs.stream().map(mapping::dto2Entity).collect(Collectors.toList());
        if (CollUtil.isNotEmpty(entityList)){
            entityList.forEach(a->{
                a.setId(UUID.randomUUID().toString().replace("-",""));
                a.setApplyBookId(applyBookId);
            });
            return this.saveBatch(entityList);
        }
        return  true;
    }

    @Override
    public HonorApplyBookMemberVO getInfoById(String id) {
        HonorApplyBookMemberEntity entity = this.getById(id);
        return mapping.entity2Vo(entity);
    }

    @Override
    public boolean add(HonorApplyBookMemberDTO dto) {
        HonorApplyBookMemberEntity entity = mapping.dto2Entity(dto);
        return this.save(entity);
    }


    @Override
    public boolean update(HonorApplyBookMemberDTO dto) {
        HonorApplyBookMemberEntity entity = mapping.dto2Entity(dto);
        return this.update(entity, null);
    }

    @Override
    public boolean batchRemove(List<String> ids) {
        return this.removeByIds(ids);
    }

    @Override
    public int deleteHonorApplyBookMemberListByApplyBookId(String applyBookId){
        return honorApplyBookMemberMapper.deleteHonorApplyBookMemberListByApplyBookId(applyBookId);
    }
}
